package com.xrit.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.xrit.constant.MessageConstant;
import com.xrit.exception.CustomException;
import com.xrit.mapper.OrderSettingMapper;
import com.xrit.pojo.OrderSetting;
import com.xrit.pojo.OrderSettingExample;
import com.xrit.service.OrderSettingService;
import com.xrit.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


/**
 * 预约管理业务实现类
 */
@Service(interfaceClass = OrderSettingService.class)
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    public OrderSetting IsExist4OrderDate(OrderSetting os) throws CustomException {
        //1.判断此日期是否已经在数据库中存在
        OrderSettingExample ose=new OrderSettingExample();
        OrderSettingExample.Criteria osec = ose.createCriteria();
        osec.andOrderdateEqualTo(os.getOrderdate());
        List<OrderSetting> list = orderSettingMapper.selectByExample(ose);
        //1.1存在，判断预约人数是否>更新的数据
        if (list!=null && list.size()>0){
            OrderSetting setting = list.get(0);
            if (setting.getReservations()!=null && setting.getReservations()>os.getNumber()){
                //预约人数>更新的数据,抛出异常
                throw new CustomException(MessageConstant.ORDERSETTING_FAIL2);
            }
            return setting;
        }
        //1.2 不存在
        return null;
    }

    /**
     * 通过日期设置最大的可预约数
     * @param orderSetting
     */
    @Override
    public void updateNumberByOrderDate(OrderSetting orderSetting) throws CustomException {
        if (IsExist4OrderDate(orderSetting)!=null) {
            OrderSettingExample ose = new OrderSettingExample();
            OrderSettingExample.Criteria osec = ose.createCriteria();
            osec.andOrderdateEqualTo(orderSetting.getOrderdate());
            //存在则修改
            //System.out.println("设置预约信息.....");
            orderSettingMapper.updateByExampleSelective(orderSetting,ose);
        } else {
            //不存在则添加
            orderSettingMapper.insertSelective(orderSetting);
        }
    }

    /**
     * 根据年份和月份获取预约设置的数据
     * @param year
     * @param month
     * @return
     */
    @Override
    public List<Map> getOrderSettingByDate(int year, int month) {
        //1.开始日期
        Date firstDayOfMonth = DateUtils.getFirstDayOfMonth(year, month);
        //2.结束日期
        Date lastDayOfMonth = DateUtils.getLastDayOfMonth(year, month);
        //3.设置查询条件
        OrderSettingExample ose=new OrderSettingExample();
        OrderSettingExample.Criteria osec = ose.createCriteria();
        osec.andOrderdateBetween(firstDayOfMonth,lastDayOfMonth);
        List<OrderSetting> settings = orderSettingMapper.selectByExample(ose);
        //4.遍历结果集
        if (settings!=null && settings.size()>0){
            /**
             * 4.1 将数据封装成下面的格式返回出去
             * { date: 1, number: 120, reservations: 1 },{......},{......}
             */
            List<Map> list=new ArrayList<Map>();
            for (OrderSetting os:settings) {
                Map map = new HashMap();
                map.put("date",os.getOrderdate().getDate());//获取到的年月日，只需要日子数
                map.put("number",os.getNumber()==null?0:os.getNumber());
                map.put("reservations",os.getReservations()==null?0:os.getReservations());
                list.add(map);
            }
            //4.2 返回新的结果集
            return list;
        }
        return null;
    }

    /**
     * 添加预约设置基础数据
     * @param orderSettings
     * @throws CustomException
     */
    @Transactional
    @Override
    public void add(List<OrderSetting> orderSettings) throws CustomException {
        //需要对orderSettings进行数据的筛选，因为需要保证数据的准确性
        //1.声明一个新的集合用于保存不存在数据库中的数据，用于新增数据
        List<OrderSetting> addList=new ArrayList<>();
        //2.循环遍历前台过来的集合
        for (OrderSetting os:orderSettings) {
            //2.1 判断此日期是否已经在数据库中存在
            OrderSetting existOrderSetting = IsExist4OrderDate(os);
            if (existOrderSetting!=null) {
                os.setId(existOrderSetting.getId());//注意：这里别丢了
                orderSettingMapper.updateByPrimaryKeySelective(os);
            }else{
                //2.1.2 不存在，需要添加的数据
                addList.add(os);
            }
        }
        //3.将新的数据集合批量添加到数据库中
        if (addList.size()>0) {
            orderSettingMapper.addBatch(addList);
        }
    }
}
